package com.bridgeintelligent.tag.user.query.service;

import cn.hutool.core.util.StrUtil;
import com.bridgeintelligent.tag.user.mgmt.entity.Result;
import com.bridgeintelligent.tag.user.mgmt.entity.UserCodes;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.user.query.entity.UserPage;
import com.bridgeintelligent.tag.user.query.mapper.UserQueryMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户服务层接口实现
 */

@Service
public class UserQueryServiceImpl implements UserQueryService {

    @Autowired
    private UserQueryMapper UserQueryMapper;
    @Autowired
    private RoleQueryService roleQueryService;


    /**
     * 校验用户名是否重复
     *
     * @param username
     * @return
     */
    @Override
    public boolean checkUserName(String username) {
        int count = UserQueryMapper.checkUserName(username);
        if (count == 1) {
            return false;
        }
        return true;
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param username
     */
    @Override
    public User findOneByUsername(String username) {
        User user = UserQueryMapper.findOneByUsername(username);
        if (user != null) {
            if (UserCodes.EFFTCTIVE_SYMBOL_01.equals(user.getEfftctiveSymbol())) {
                user.setIsEfftctive("有效");
            } else if (UserCodes.EFFTCTIVE_SYMBOL_02.equals(user.getEfftctiveSymbol())) {
                user.setIsEfftctive("无效");
            }
            if (!StrUtil.isBlank(user.getRoleId())){
               Map<String,String> result = new HashMap<>();
                List<String> roleList = Arrays.asList(user.getRoleId().split(","));
                for (String roleId:roleList) {
                    String roleName = roleQueryService.findRoleNameById(roleId);
                    result.put(roleId,roleName);
                }
                user.setRoleMap(result);
            }
        }
        return user;
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public List<User> findAllUser() {
        List<User> list = UserQueryMapper.findAllUser();
        if (list != null) {
            for (User user : list) {
                if (UserCodes.EFFTCTIVE_SYMBOL_01.equals(user.getEfftctiveSymbol())) {
                    user.setIsEfftctive("有效");
                } else if (UserCodes.EFFTCTIVE_SYMBOL_02.equals(user.getEfftctiveSymbol())) {
                    user.setIsEfftctive("无效");
                }
                if (!StrUtil.isBlank(user.getRoleId())){
                    Map<String,String> result = new HashMap<>();
                    List<String> roleList = Arrays.asList(user.getRoleId().split(","));
                    for (String roleId:roleList) {
                        String roleName = roleQueryService.findRoleNameById(roleId);
                        result.put(roleId,roleName);
                    }
                    user.setRoleMap(result);
                }
            }
        }
        return list;
    }

    @Override
    public List<User> findUserList() {
        return UserQueryMapper.findUserList();
    }

    @Override
    public List<User> findAllUserList() {
        return UserQueryMapper.findAllUserList();
    }

    /**
     * 获取用户列表
     *
     * @param pageNumber
     * @param pageSize
     * @param name
     * @param userPhone
     * @return
     */
    @Override
    public Result list(Integer pageNumber, Integer pageSize, String name, String userPhone, String username) {
        Page page = PageHelper.startPage(pageNumber, pageSize);
        List<User> list = UserQueryMapper.findList(name, userPhone, username);
        if (list == null) {
            return null;
        }
        for (User user : list) {
            if (UserCodes.EFFTCTIVE_SYMBOL_01.equals(user.getEfftctiveSymbol())) {
                user.setIsEfftctive("有效");
            } else if (UserCodes.EFFTCTIVE_SYMBOL_02.equals(user.getEfftctiveSymbol())) {
                user.setIsEfftctive("无效");
            }
            if (!StrUtil.isBlank(user.getRoleId())){
                Map<String,String> result = new HashMap<>();
                List<String> roleList = Arrays.asList(user.getRoleId().split(","));
                for (String roleId:roleList) {
                    String roleName = roleQueryService.findRoleNameById(roleId);
                    result.put(roleId,roleName);
                }
                user.setRoleMap(result);
            }
        }
        UserPage<User> userPage = new UserPage<>();
        userPage.setContent(list);
        userPage.setTotalPages(page.getPages());
        userPage.setTotalElements(page.getTotal());
        userPage.setSize(page.getPageSize());
        userPage.setRequestPager(pageNumber);
        return new Result(0, userPage);
    }

    /**
     * 根据用户id查询用户姓名
     *
     * @param createrId
     * @return
     */
    @Override
    public String findOneByUserId(String createrId) {
        return UserQueryMapper.findOneByUserId(createrId);
    }

    /**
     * 根据用户id查询用户
     *
     * @param userId
     * @return
     */
    @Override
    public User findByUserId(String userId) {
        return UserQueryMapper.findByUserId(userId);
    }

}
